<template>
  <div class="changePwd-container">
    <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
      <el-form-item label="原 密 码" prop="pass">
        <el-input type="password" size="medium" v-model="ruleForm2.pass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="新 密 码" prop="newPass">
        <el-input type="password" size="medium" v-model="ruleForm2.newPass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="重复密码" prop="checkPass">
        <el-input type="password" size="medium" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
        <el-button type="info" @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script >
export default {
  data() {
    var checkPass = (rule, value, callback) => {
      if (value == '') {
        return callback(new Error('请输入原密码'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'));
      } else {
        if (this.ruleForm2.checkPass !== '') {
          this.$refs.ruleForm2.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入再次输入新密码'));
      } else if (value !== this.ruleForm2.newPass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    }
    return {
      ruleForm2: {
        pass: '',
        newPass: '',
        checkPass: ''
      },
      rules2: {
        pass: [
          { validator: checkPass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        newPass: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            await this.$post('/api/User/ChangePassword', {
              oldPassword: this.ruleForm2.pass,
              newPassword: this.ruleForm2.newPass
            })
            this.$message({
              message: '恭喜你，密码修改成功',
              type: 'success'
            })
          } catch (error) {
            this.$message.error(error.message)
          }
        } else {
          return false;
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    this.showHome.isShow = false;
  }
}
</script>
<style lang="scss" scoped>
.changePwd-container {
  width: 100%;
  height: 100%;
  display: flex;
  padding-top:30px;
  // justify-content: center;
  // align-items: center;
}
</style>